let sdl_init () =
  Sdl.init [`EVERYTHING]

(* Image sizes *)
let width img = (Sdlvideo.surface_info !img).Sdlvideo.w
let height img = (Sdlvideo.surface_info !img).Sdlvideo.h

(* Write a list in a specified file *)
let rec writePoints list file =
  match list with
    |[] -> ()
    |(x, y)::l -> output_string file ("(" ^ string_of_int(x) ^ "," ^ string_of_int(y) ^ ")\n"); writePoints l file


(* Write a vertex list in a specified file *)
let rec writeListVert list file =
  match list with
    |[] -> ()
    |(a,b,c)::l -> begin output_string file ("v " ^ string_of_int(a) ^ " " ^ string_of_int(b)^" " ^ string_of_int(c)^"\n"); writeListVert l file end

(* Write a face list in a specified file *)
let rec writeListFaces list file =
  match list with
    |[] -> ()
    |(a,b,c)::l -> begin output_string file ("f " ^ string_of_int(a) ^ " " ^ string_of_int(b)^" " ^ string_of_int(c)^"\n"); writeListFaces l file end

(* Write a string list in a specified file *)
let rec writeColors list file = 
  match list with
    |[] -> ()
    |(a,b,c)::l -> begin output_string file ("( " ^ string_of_int a ^ ", " ^ string_of_int b ^ ", " ^ string_of_int c ^ ")\n"); writeColors l file end

(* Checks if a specified element is already in the list *)
let rec contains a = function
  |[] -> false
  |e::l -> e = a || contains a l

(* Write a list containing every color in the initial picture *)
let write_colors surface =
  let file = open_out "files/colors" in
  let color_list = ref [] in
    for y = 0 to (height surface) - 1 do
      for x = 0 to (width surface) - 1 do
	let (r,g,b) = Sdlvideo.get_pixel_color !surface ~x:x ~y:y in
	  if not(contains (r,g,b) !color_list) then
	    color_list := (r,g,b)::!color_list
      done;
    done;
    writeColors !color_list file;
    close_out file

(* Get altitude from x and y in the picture *)
let getAltitude (surface, x, y) =
  let color = Sdlvideo.get_pixel_color !surface ~x:x ~y:y in
  match color with
  |(0, 0, 255) -> 0;
  |(0, 63, 0) -> 20;
  |(0, 127, 0) -> 40;
  |(0, 191, 0) -> 60;
  |(0, 255, 0) -> 80;
  |(0, 255, 127) -> 100;
  | _ -> 20
      

(* Write the .obj file *)
let writeVertex pas surface =
  let (x,y) = Img_process.get_dims !surface in
  let file = open_out "files/fichier.obj" in
  let compteur = ref 0 in
  let list_vert = ref [] in  
  let list_faces = ref [] in  
    for i = 0 to (x - 1) do 
      for j = 0 to (y - 1) do
	if((x - 1 - i) mod pas = 0 && (y - 1 - j) mod pas = 0 && (x - 1 - i) >= pas && (y - 1 - j) >= pas) then
	  begin
	    list_vert := (x - 1 - i - pas,getAltitude(surface,x - 1 - i - pas,y - 1 - j),y - 1 -j):: !list_vert;
	    list_vert := (x - 1 - i,getAltitude(surface,x - 1 - i,y - 1 - j), y - 1 - j):: !list_vert;
	    list_vert := (x - 1 - i - pas,getAltitude(surface,x - 1 - i- pas,y - 1 - j- pas),y - 1 - j - pas)::!list_vert;
	    list_faces := (!compteur + 1  ,!compteur + 2,!compteur+3):: !list_faces;
	    list_vert := (x - 1 - i,getAltitude(surface,x - 1 - i,y - 1 - j- pas),y - 1 - j - pas):: !list_vert;
	    list_vert := (x - 1 - i,getAltitude(surface,x - 1 - i,y - 1 - j),y - 1 - j):: !list_vert;
	    list_vert := (x - 1 - i - pas,getAltitude(surface,x - 1 - i- pas,y - 1 - j- pas),y - 1 - j - pas)::!list_vert;
	    list_faces := (!compteur + 4 ,!compteur + 5 ,!compteur + 6):: !list_faces;
	    compteur := !compteur + 6
	  end
      done;
    done;
    writeListVert (List.rev !list_vert) file;
    writeListFaces ( !list_faces) file;
    close_out file

let rec detectInter img output = 
  let (width, height) = Img_process.get_dims !img in
  let file = open_out output in
  let interList = ref [] in
    for i = 0 to width - 1 do
      for j = 0 to height - 1 do
	let color = Sdlvideo.get_pixel_color !img i j in
	  if color = Img_process.inter_color then
	    interList := (i, j)::!interList
      done;
    done;
    writePoints !interList file;
    close_out file

let generate surface pas = 
  write_colors surface;
  writeVertex pas surface;
  detectInter surface "files/intersections"
